package org.scirp.ijmis.dao.personpaper.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.SQLQuery;
import org.scirp.ijmis.dao.base.impl.BaseDAOImpl;
import org.scirp.ijmis.dao.personpaper.IPaperStatusDAO;
import org.scirp.ijmis.po.personpaper.TPaperinformation;
import org.scirp.ijmis.po.personpaper.TStatusofpaper;
import org.scirp.ijmis.util.Pagination;

public class PaperStatusDAOImpl extends BaseDAOImpl<TPaperinformation> implements IPaperStatusDAO {

	private static final String className = PaperStatusDAOImpl.class.toString();

	public Long doInsertStartState(TPaperinformation tpi) {
		return super.saveObject(tpi, "");
	}

	public void deletePaperinformationByPaperId(Long pid) {
		String hql = "delete from TPaperinformation where paperid=:paperid";
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("paperid", pid);
		super.ExecuteNonQuery(hql, parameters, className + "-->deletePaperinformationByPaperId(Long pid)");
	}

	public int countPaperByStatus(int i, Long journalId) {
		String hql = "select count(*) from TPaperinformation where journalid=:journalid and statusid=:statusid";
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("journalid", journalId);
		parameters.put("statusid", Long.parseLong(String.valueOf(i)));
		List list = super.ExecuteQuery(hql, parameters, className + "-->countPaperByStatus(int i, Long journalId)");
		return Integer.parseInt(list.get(0).toString());
	}

	public List<TPaperinformation> selectPaperByStatusId(int statusId, Long journalId, Pagination pagination) {
		String hql = " from TPaperinformation where statusid=" + statusId + " and journalid=" + journalId + " order by paperno desc";
		if (pagination == null) {
			return select(hql, 0, 0, className + "-->selectPaperByStatusId(statusId, journalId, pagination)");
		} else {
			return select(hql, pagination.getCurrentPage(), pagination.getPageSize(), className + "-->selectPaperByStatusId(Long journalId)");
		}
	}

	public boolean updatePaperStatus(Long paperId, Long statusId, String remark) {
		String hql = "update from TPaperinformation set statusid=:statusid,operatetime=sysdate,remark=:remark where paperid=:paperid ";
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("statusid", statusId);
		parameters.put("paperid", paperId);
		parameters.put("remark", remark);
		return super.ExecuteNonQuery(hql, parameters, className + "-->updatePaperStatus(Long paperId, Long statusId,String remark)");
	}

	public int countRemindPaperByStatus(int statusId, Long journalId) {
		String hql = "select count(*) from TPaperinformation t1,TStatusofpaper t2 where (t1.statusid=t2.pid and t2.remindertime<>0 and t2.warningtime<>0 and t1.journalid=:journalid and statusid=:statusid and sysdate>t1.operatetime+t2.remindertime and sysdate<=t1.operatetime+t2.warningtime) or	(t1.statusid=t2.pid and	t2.remindertime<>0 and t2.warningtime=0 and	t1.journalid=:journalid and statusid=:statusid and sysdate>t1.operatetime+t2.remindertime)";
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("journalid", journalId);
		parameters.put("statusid", statusId);
		List list = super.ExecuteQuery(hql, parameters, className + "-->countRemindPaperByStatus(int statusId, Long journalId)");
		return Integer.parseInt((list.get(0)).toString());
	}

	public int countWarnPaperByStatus(int statusId, Long journalId) {
		String hql = "select count(*) from TPaperinformation t1,TStatusofpaper t2 where t1.statusid=t2.pid and t2.warningtime<>0 and t1.journalid=:journalid and statusid=:statusid and sysdate>t1.operatetime+t2.warningtime ";
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("journalid", journalId);
		parameters.put("statusid", statusId);
		List list = super.ExecuteQuery(hql, parameters, className + "-->countWarnPaperByStatus(int statusId, Long journalId)");
		return Integer.parseInt((list.get(0)).toString());
	}

	public String checkOvertime(Long pid) {
		String hql1 = "select count(*) from TPaperinformation t1,TStatusofpaper t2 where (t1.statusid=t2.pid and t2.remindertime<>0 and t1.pid=" + pid + " and sysdate>t1.operatetime+t2.remindertime and sysdate<=t1.operatetime+t2.warningtime) or (t1.statusid=t2.pid and t2.remindertime<>0 and t2.warningtime=0 and t1.pid=" + pid + " and sysdate>t1.operatetime+t2.remindertime)";
		if (Integer.parseInt(super.getHibernateTemplate().find(hql1).get(0).toString()) == 1)
			return "remind";
		else {
			String hql2 = "select count(*) from TPaperinformation t1,TStatusofpaper t2 where t1.statusid=t2.pid and t2.warningtime<>0 and t1.pid=" + pid + " and sysdate>t1.operatetime+t2.warningtime";
			if (Integer.parseInt(super.getHibernateTemplate().find(hql2).get(0).toString()) == 1)
				return "warn";
		}
		return "normal";
	}

	public boolean updatePaperStatus(Long paperId, Long statusId, String feeType, String remark) {
		String hql = "update from TPaperinformation set statusid=:statusid,operatetime=sysdate,paperfeeremark=:paperfeeremark,remark=:remark where paperid=:paperid ";
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("statusid", statusId);
		parameters.put("paperid", paperId);
		parameters.put("remark", remark);
		parameters.put("paperfeeremark", feeType);
		return super.ExecuteNonQuery(hql, parameters, className + "-->updatePaperStatus(Long paperId, Long statusId, String feeType,String remark)");
	}

	public boolean updatePaperStatus(Long paperId, Long statusId, Long vol, Long no, String remark) {
		String hql = "update from TPaperinformation set statusid=" + statusId + ",operatetime=sysdate,vol=" + vol + ",no=" + no + ",remark=:remark" + " where paperid=" + paperId;
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("remark", remark);
		return super.ExecuteNonQuery(hql, parameters, className + "-->updatePaperStatus(Long paperId, Long statusId,Long vol,Long no,String remark)");
	}

	public List<TPaperinformation> selectPaperByPaperNo(Long paperno, Long journalId) {
		String hql = " from TPaperinformation where paperno=" + paperno + " and journalid=" + journalId;
		return super.ExecuteQuery(hql, className + "-->selectPaperByPaperNo(Long journalId)");
	}

	public List selectPaperByPaperId(Long paperid) {
		String hql = " from TPaperinformation where paperid=" + paperid;
		return super.ExecuteQuery(hql, className + "-->selectPaperByPaperId(Long journalId)");
	}

	public int saveNotExistPaperInfo(String date) {
		String sql = "insert into t_paperinformation(pid, journalid, operatetime, paperid, paperno, statusid) select paperinformation_sequence.nextval,t_person.journalid,to_date(t_paper.submissiondatetime,'yyyy-MM-dd HH24:mi:ss'),t_paper.pid,t_paper.paperno,1 from t_paper,t_person where t_person.pid=t_paper.personid and t_paper.submissiondatetime>'" + date + "' and not exists(select 1 from t_paperinformation where paperid=t_paper.pid)";
		SQLQuery query = super.getSession().createSQLQuery(sql);
		return query.executeUpdate();
	}

	@Override
	public List countPaperByAllStatus(List<TStatusofpaper> statusList, Long journalid) {
		StringBuffer sql = new StringBuffer();
		sql.append("select ");
		for (TStatusofpaper status : statusList) {
			sql.append("(select count(*) from t_paperinformation t where t.statusid=" + status.getPid() + " and t.journalid=" + journalid + ") as " + status.getStatusname() + ",");
		}
		SQLQuery query = super.getSession().createSQLQuery(sql.substring(0, sql.length() - 1) + " from dual");
		return query.list();
	}

	@Override
	public int countViewPaperByVolNo(Long vol, Long no, Long journalId) {
		String hql;
		if (vol != 0) {
			if (no != 0)
				hql = "select count(*) from TPaperinformation where statusid=12 and journalid=" + journalId + " and vol=" + vol + " and no=" + no;
			else
				hql = "select count(*) from TPaperinformation where statusid=12 and journalid=" + journalId + " and vol=" + vol;
		} else if (no != 0) {
			hql = "select count(*) from TPaperinformation where statusid=12 and journalid=" + journalId + " and no=" + no;
		} else
			hql = "select count(*) from TPaperinformation where statusid=12 and journalid=" + journalId +" and (vol=0 or vol is null) and (no=0 or no is null)";
		List list = super.ExecuteQuery(hql, className + "-->countViewPaperByVolNo(Long vol, Long no, Long journalId)");
		return Integer.parseInt(list.get(0).toString());
	}

	public List<TPaperinformation> selectViewPaperByVolNo(Long vol, Long no, Long journalId, Pagination pagination) {
		String hql;
		if (vol != 0) {
			if (no != 0)
				hql = " from TPaperinformation where statusid=12 and journalId=" + journalId + " and vol=" + vol + " and no=" + no;
			else
				hql = " from TPaperinformation where statusid=12 and journalId=" + journalId + " and vol=" + vol;
		} else if (no != 0) {
			hql = " from TPaperinformation where statusid=12 and journalId=" + journalId + " and no=" + no;
		} else
			hql = " from TPaperinformation where statusid=12 and journalId=" + journalId + " and (vol=0 or vol is null) and (no=0 or no is null)";
		if (pagination == null) {
			return select(hql, 0, 0, className + "-->selectViewPaperByVolNo(Long vol, Long no, Long journalId,Pagination pagination)");
		} else {
			return select(hql, pagination.getCurrentPage(), pagination.getPageSize(), className + "-->selectViewPaperByVolNo(Long vol, Long no, Long journalId,Pagination pagination)");
		}
	}

	public boolean updatePaperStatus(String ids, Long nextStatusId) {
		String hql = "update from TPaperinformation set statusid=" + nextStatusId + ",operatetime=sysdate where paperid in (" + ids + ")";
		return super.ExecuteNonQuery(hql, className + "-->updatePaperStatus(String ids, Long nextStatusId)");
	}
}
